<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称">
        {{ spu.spuName }}
      </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input placeholder="价格(元)" v-model="skuInfo.price" type="number"></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)必填" v-model="skuInfo.weight" type="number"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input type="textarea" rows="4" v-model="skuInfo.skuDesc"></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item :label="attr.attrName" v-for="(attr,index) in attrInfoList" :key="index" >
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option :label="attrValue.valueName" :value="`${attr.id}:${attrValue.id}`" v-for="(attrValue,index) in attr.attrValueList" :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" class="demo-form-inline">
          <el-form-item :label="sale.saleAttrName" v-for="(sale,index) in spuSaleAttrList" :key="index">
            <el-select placeholder="请选择" v-model="sale.attrIdAndValueId">
              <el-option :label="saleValue.saleAttrValueName" :value="`${sale.id}:${saleValue.id}`" v-for="(saleValue,index) in sale.spuSaleAttrValueList" :key="index"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-table border data="" style="width: 100%" :data="spuImageList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" prop="date" label="日期" width="80px"></el-table-column>
          <el-table-column prop="imgUrl" label="图片" width="width">
            <template slot-scope="scope">
              <img :src="scope.row.imgUrl" style="width:100px;height:100px">
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width"></el-table-column>
          <el-table-column prop="date" label="操作" width="width">
            <template slot-scope="scope">
              <el-button type="primary" v-if="scope.row.isDefault==0" @click="setingDefaultImg(scope.row)">设置默认</el-button>
              <el-button type="success" v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveSkuInfo">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>

export default {
  name: "SkuForm",
  data() {
    return {
      //存储图片的信息
      spuImageList: [],
      //存储销售是属性
      spuSaleAttrList: [],
      //存储平台属性的数据
      attrInfoList: [],
      //skuInfo
      skuInfo: {
        //第一类收集的数据：父组件给的数据
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        //第二类：需要通过数据双向绑定v-model收集
        skuName: "",
        price: 0,
        weight: "0",
        skuDesc: "",
        //第三类：需要自己书写代码
        //默认图片
        skuDefaultImg: "",
        //收集图片的字段
        skuImageList: [
          // {
          //   id: 0,
          //   imgName: "string",
          //   imgUrl: "string",
          //   isDefault: "string",
          //   skuId: 0,
          //   spuImgId: 0,
          // },
        ],
        //平台属性
        skuAttrValueList: [
          // {
          //   attrId: 0,
          //   valueId: 0,
          // },
        ],
        //销售属性
        skuSaleAttrValueList: [
          // {
          //   id: 0,
          //   saleAttrId: 0,
          //   saleAttrName: "string",
          //   saleAttrValueId: 0,
          //   saleAttrValueName: "string",
          //   skuId: 0,
          //   spuId: 0,
          // },
        ],
      },
      //spu
      spu:{},
      //存储图片表格中勾选的图片列表
      imageList:[],
    }
  },
  methods: {
    async initSkuData(category1Id, category2Id, row) {
      this.spu=row;
      this.skuInfo.category3Id = row.category3Id;
      this.skuInfo.spuId = row.id;
      this.skuInfo.tmId = row.tmId;
      //派发请求，获取参数
      //获取图片列表数据
      const result = await this.$API.default.spu.reqSpuImageList(row.id);
      if (result.code == 200) {
        const List = result.data;
        List.forEach((item)=>{
          item.isDefault=0;
        })
        this.spuImageList=List;
      }
      //获取销售属性列表数据
      const result1 = await this.$API.default.spu.reqSpuSaleAttrList(row.id);
      if (result1.code == 200) {
        this.spuSaleAttrList = result1.data;
      }
      //获取spuInfo属性列表数据
      const result2 = await this.$API.default.spu.reqSpuInfoList(category1Id, category2Id, row.category3Id);
      if (result2.code == 200) {
        this.attrInfoList = result2.data;
      }
    },
    //存储勾选的图片列表 SelectionChange回调
    handleSelectionChange(row){
      this.imageList=row;
    },
    //设置默认按钮的回调
    setingDefaultImg(row){
      this.spuImageList.forEach((item)=>{
        item.isDefault=0;
      })
      row.isDefault=1;
      this.skuInfo.skuDefaultImg=row.imgUrl;
    },
    //取消按钮的回调
    cancel(){
      this.$emit('cancelSkuForm',{scene:0,page:undefined})
      //清除数据
      Object.assign(this._data,this.$options.data());
    },
    //保存按钮的回调
    async saveSkuInfo(){
      //整理参数
      const {skuInfo,imageList,spuSaleAttrList,attrInfoList}=this;
      //整理平台参数
      skuInfo.skuAttrValueList=attrInfoList.reduce((prev,item)=>{
        if(item.attrIdAndValueId){
          const [attrId,valueId]=item.attrIdAndValueId.split(":");
          const obj={attrId,valueId};
          prev.push(obj);
        }
        return prev;
      },[])
      //整理销售属性参数
      skuInfo.skuSaleAttrValueList=spuSaleAttrList.reduce((prev,item)=>{
        if(item.attrIdAndValueId){
          const [saleAttrId,saleAttrValueId]=item.attrIdAndValueId.split(":");
          prev.push({saleAttrId,saleAttrValueId});
        }
        return prev;
      },[])
      //整理图片参数
      skuInfo.skuImageList=imageList.map(item=>{
        return{
          imgName:item.imgName,
          imgUrl:item.imgUrl,
          isDefault:item.isDefault,
          spuImgId:item.id,
        }
      });
      //发请求
      const result=await this.$API.default.spu.reqSaveSkuInfo(skuInfo);
      console.log(result)
      if(result.code==200){
        this.$emit('cancelSkuForm', {scene:0,page:undefined})

        this.$message({
          type:'success',
          message:'SKU保存成功'
        })
      }
      //清除数据
      Object.assign(this._data,this.$options.data())
    },

  }
}
</script>

<style scoped>

</style>
